#include "poweroff.h"
#include "ui_poweroff.h"
#include <QMessageBox>
#include "msgbox.h"
#include "frminput2019.h"
#include "userlogger.h"

PowerOff::PowerOff(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PowerOff)
{
    bdf = BllDataFlow::getInstance();
    bCtrl = BllController::getInstance();

    //Signals and Slots
    connect(bdf, &BllDataFlow::endExperimentSignal, this, &PowerOff::setEnableSlot);
    connect(bdf, &BllDataFlow::retranslateUiSignal, this, &PowerOff::retranslateUiSlot);
    ui->setupUi(this);

    if (windowWidth >= 1280) {
        this->setProperty("cssSize", "large");
        ui->btnPowerOff->setIconSize(QSize(36,39));
    }
}

PowerOff::~PowerOff()
{
    delete ui;
}

void PowerOff::on_btnPowerOff_clicked()
{
    frmInput2019::Instance()->hideAndClearFocus();

    MsgBox msgBox;
    msgBox.setWindowTitle(QString(tr("PowerOff")));
    msgBox.setTitle(QString(tr("Power Off")));
    msgBox.setText(QString(tr("Would you like to power off?")));
    int ret = msgBox.exec();
    if (ret == QMessageBox::Ok)
    {
        QString message = tr("Shutting down");
        ComCmd cmd = {PWROFF, 0, 0, 0, 0, 0};
        bCtrl->sendPortCmd(cmd);
        UserLogger::log(message);
        Loading::showLoading(QColor(0,0,0,200), false, message);
    }
}

void PowerOff::setEnableSlot(bool state)
{
    ui->btnPowerOff->setEnabled(state);
    ui->btnPowerOff->setVisible(state);
    this->setVisible(state);
}

void PowerOff::retranslateUiSlot()
{
    ui->retranslateUi(this);
}
